/**
 * 
 */
package com.acc.softmgr.business.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.acc.softmgr.business.model.SysMenu;
import com.acc.softmgr.business.model.SysMenuExample;
import com.acc.softmgr.business.service.ISysMenuService;
import com.acc.softmgr.common.constant.DeleteStatus;
import com.acc.softmgr.common.constant.enums.LeafEnums;
import com.acc.softmgr.common.constant.enums.RestStatus;
import com.acc.softmgr.common.constant.enums.SpreadEnums;
import com.acc.softmgr.common.utils.string.StringUtil;
import com.acc.softmgr.common.web.ResponseObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月12日
 * 
 * @author OprCalf
 * 
 */

@Controller
@RequestMapping("sysMenu")
public class SysMenuController {

	@Autowired
	private ISysMenuService sysMenuService;

	@RequestMapping(value = "toSysMenu", method = RequestMethod.GET)
	public String toSysMenu(Model model) {
		return "sysMenu/sysMenu";
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchMenu", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject searchMenu(@PathVariable Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		SysMenu sysMenu = sysMenuService.selectByPrimaryKey(id);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		responseObject.setData(sysMenu);
		return responseObject;
	}

	/**
	 * 新增菜单
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addMenu", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject addMenu(SysMenu sysMenu) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (sysMenu.getParent_id() != null && sysMenu.getParent_id() > 0) {
			SysMenu parentSysMenu = sysMenuService.selectByPrimaryKey(sysMenu.getParent_id());// 查询父节点
			if (parentSysMenu != null && parentSysMenu.getId() != null) {
				parentSysMenu.setIs_leaf(LeafEnums.NO_LEAF.getCode());
				parentSysMenu.setSpread(SpreadEnums.TRUE.getCode());
				sysMenuService.updateByPrimaryKey(parentSysMenu);
			}
		} else {
			sysMenu.setParent_id(0);
		}
		if (StringUtil.isEmpty(sysMenu.getHref())) {
			sysMenu.setHref(" ");
		}
		sysMenu.setDeleted(DeleteStatus.FALSE);
		sysMenuService.insert(sysMenu);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 修改
	 * 
	 * @param psMenu
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editMenu", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject editMenu(SysMenu sysMenu) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(sysMenu.getHref())) {
			sysMenu.setHref("");
		}
		sysMenuService.updateByPrimaryKey(sysMenu);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/removeMenu", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject removeMenu(@Validated Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		SysMenu sysMenu = sysMenuService.selectByPrimaryKey(id);
		List<SysMenu> sonMenus = getSonMenus(id);
		if (sonMenus != null && sonMenus.size() > 0) {// 不能删除根节点
			responseObject.setStatus(RestStatus.EXIST_NODE.code);
			responseObject.setMessage(RestStatus.EXIST_NODE.message);
			return responseObject;
		} else {
			sysMenu.setDeleted(DeleteStatus.TRUE);
			sysMenuService.updateByPrimaryKey(sysMenu);

			SysMenu parentSysMenu = sysMenuService.selectByPrimaryKey(sysMenu.getParent_id());// 查询父节点
			if (parentSysMenu != null) {
				SysMenuExample example = new SysMenuExample();
				example.createCriteria().andParent_idEqualTo(sysMenu.getParent_id())
						.andDeletedEqualTo(DeleteStatus.FALSE);
				List<SysMenu> psMenus = sysMenuService.selectByExample(example);// 查询这个节点是否有子节点
				if (psMenus == null || psMenus.size() == 0) {// 节点没有子节点
					parentSysMenu.setIs_leaf(LeafEnums.LEAF.getCode());
					parentSysMenu.setSpread(SpreadEnums.FALSE.getCode());
					sysMenuService.updateByPrimaryKey(parentSysMenu);
				}
			}
		}

		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 查询所有
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/searchMenus", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<SysMenu> searchMenus() throws Exception {
		SysMenuExample example = new SysMenuExample();
		example.createCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SysMenu> psMenus = sysMenuService.selectByExample(example);// 查询所有菜单
		if (psMenus != null && psMenus.size() > 0) {
			List<SysMenu> results = buildTree(psMenus);
			return results;
		}
		return new ArrayList<SysMenu>();
	}

	/**
	 * 创建树
	 * 
	 * @param psMenus
	 */
	private List<SysMenu> buildTree(List<SysMenu> sysMenus) {
		List<SysMenu> firstMenus = new ArrayList<SysMenu>();
		for (SysMenu node : sysMenus) {
			if (node.getParent_id() == null || node.getParent_id() == 0) {
				firstMenus.add(node);
			}
		}
		sysMenus.removeAll(firstMenus);
		buildMenuTree(firstMenus, sysMenus);
		return firstMenus;
	}

	private void buildMenuTree(List<SysMenu> firstMenus, List<SysMenu> menus) {
		List<SysMenu> copyMenus = new ArrayList<SysMenu>(menus);
		for (SysMenu menu : firstMenus) {
			for (SysMenu m : copyMenus) {
				if (m.getParent_id().equals(menu.getId())) {
					menu.getChildren().add(m);
					// copyMenus.remove(m);
				}
			}
		}
		for (SysMenu menu : firstMenus) {
			if (menu.getChildren() != null && menu.getChildren().size() > 0) {
				buildMenuTree(menu.getChildren(), copyMenus);
			}
		}
	}

	/**
	 * 根据父节点ID获取所有的子菜单
	 * 
	 * @param id
	 */
	private List<SysMenu> getSonMenus(Integer id) {
		SysMenuExample example = new SysMenuExample();
		example.createCriteria().andParent_idEqualTo(id).andDeletedEqualTo(DeleteStatus.FALSE);
		List<SysMenu> list = sysMenuService.selectByExample(example);
		return list;
	}
}
